package com.kejiguan.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.kejiguan.ui.R;

/**
 * Created by YYY on 2016/7/15.
 */
public class CommonDialog extends Dialog implements View.OnClickListener{

    private View mDialogView;
    private TextView commonText;
    private Button confirmBtn, cancelBtn;
    private View closeView;
    private int clickableColor,unClickableColor;

    public CommonDialog(Context context) {
        super(context);
        init(context);
    }

    public CommonDialog(Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    protected CommonDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);

        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
        setCanceledOnTouchOutside(true);
    }

    private void init(Context context) {

        clickableColor=context.getResources().getColor(R.color.text_color2);
        unClickableColor=context.getResources().getColor(R.color.text_color1);

        mDialogView = View.inflate(context, R.layout.dialog_common_dialog_layout, null);
        commonText = (TextView) mDialogView.findViewById(R.id.common_text);
        confirmBtn = (Button) mDialogView.findViewById(R.id.dialog_confirm_btn);
        cancelBtn = (Button) mDialogView.findViewById(R.id.dialog_cancel_btn);
        closeView = mDialogView.findViewById(R.id.close_btn);
        closeView.setOnClickListener(this);
        setContentView(mDialogView);
        setCanceledOnTouchOutside(true);
    }

    public CommonDialog withBtnTxt(String left, String right) {
        confirmBtn.setText(left);
        cancelBtn.setText(right);
        return this;
    }

    public CommonDialog withMessage(String message) {
        commonText.setText(TextUtils.isEmpty(message) ? "" : message);
        return this;
    }

    public CommonDialog withMessage(SpannableString message) {
        commonText.setText(TextUtils.isEmpty(message) ? "" : message);
        return this;
    }



    public CommonDialog withOnClick(View.OnClickListener onClick) {
        confirmBtn.setOnClickListener(onClick);
        cancelBtn.setOnClickListener(onClick);
        return this;
    }

    public CommonDialog withTag(int tag) {
        confirmBtn.setTag(R.id.recyclerview_item_position, tag);
        cancelBtn.setTag(R.id.recyclerview_item_position, tag);
        return this;
    }


    public CommonDialog leftRightClickable(boolean leftClickable,boolean rightClickable) {
        confirmBtn.setClickable(leftClickable);
        if (leftClickable)
        {
            confirmBtn.setBackgroundResource(R.drawable.kjg_item_display_bg2);
            confirmBtn.setTextColor(clickableColor);
        }else {
            confirmBtn.setBackgroundResource(R.drawable.kjg_item_display_bg1);
            confirmBtn.setTextColor(unClickableColor);
        }

        if (rightClickable)
        {
            cancelBtn.setBackgroundResource(R.drawable.kjg_item_display_bg2);
            cancelBtn.setTextColor(clickableColor);
        }else {
            cancelBtn.setBackgroundResource(R.drawable.kjg_item_display_bg1);
            cancelBtn.setTextColor(unClickableColor);
        }

        return this;
    }

    @Override
    public void onClick(View v) {


        switch (v.getId())
        {
            case R.id.close_btn:
                dismiss();
                break;
        }
    }
}
